<?php

/**
 * ProductcategoryController
 */
class Globalconfig_ProductcategoryController extends Zend_Controller_Action
{
    public function init()
    {
        $this->params = $this->getRequest()->getParams();		
		//System_Template::using("backend", "Admin");
		//System_Template::using("getit", "Admin");
        $this->model = new Globalconfig_Model_Productcategory();
		parent::init();
    }

    public function indexAction()
    {
		$this->view->maintitle = "Danh sách loại sản phẩm";
		$model = new Globalconfig_Model_Productcategory();
		
		$id = $this->_request->getParam("id");
		$name = $this->_request->getParam("name");
		$parentid = $this->_request->getParam("parentid"); 
		$storeid = $this->_request->getParam("storeid");
		
		$this->view->id = $id;
		$this->view->name = $name;
		$this->view->parentid = $parentid;
		$this->view->storeid = $storeid;
		$parameter = array('id' => $id, 'name' => $name, 'parentid' => $parentid,
				'storeid' => $storeid);
		
		$count = $model->count($parameter);
		$paging = common::pagingAuto($count);
		$data = $model->SelectAll($parameter, $paging[0], $paging[1]);
		//Lấy danh sách store category ở đây để điền vào Combobox		
		$this->storectgmodel = new Globalconfig_Model_Storecategory();	
		$this->view->listStoreCategory = $this->storectgmodel->SelectAllNoPaging();
		$this->view->data = $data;
		// common::showButton('delete');
		// common::showButton('add');
    }
	
	public function inputAction() {
		// common::showButton('save');
		// common::showButton('back');
		$this->storeModel = new Storemanagement_Model_Store();
		$this->model = new Globalconfig_Model_Productcategory();	
		$id = $this->_getParam("id");
		$this->view->flag = 0;
		if ($id == 0) {
			$this->view->maintitle = "Thêm loại sản phẩm vào danh sách";
			
			//Lấy danh sách product category ở đây để điền vào Combobox
			$this->view->listparent = $this->model->SelectAllNoPaging();
		} else {
			$this->view->maintitle = "Cập nhật thông tin loại sản phẩm";
			$data = $this->model->FindById($id);
			$this->view->data = $data;
			//Lấy danh sách product category ở đây để điền vào Combobox	
			$this->view->listparent = $this->model->SelectAllNoPagingById($id);
		}
		$this->view->listStore = $this->storeModel->SelectAllNoPaging();
		$this->view->id = $id;
			
        $this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender(false);	
	}
	
	public function saveAction() {
		$uploader = new System_Upload();
		$uploadInfo = $uploader->upload();
		$id = $this->_getParam("id");
		$name = $this->_request->getParam("name");
		$parentid = $this->_request->getParam("parentid"); 
		if($parentid == '')
			$parentid = 0;
		$storeid = $this->_request->getParam("storeid");
		 
		$param = array('productcategoryid' => $id, 'name' => $name, 'parentid' => $parentid,
				'storeid' => $storeid);
		$this->model->CheckExitsID($id);
		$this->model->AddNewProductCategory($param);			
		
		$this->_redirect('/globalconfig/productcategory/index');
		
		}
	
	public function deleteAction() {
		$params = $this->getRequest()->getParams();
		$db = Zend_Db_Table::getDefaultAdapter();
		$ids = $params["DEL"];
		for ($i = 0; $i < count($ids); $i++) {
			$this->model->DeleteProductCategory($ids[$i]);
		}
		$this->_redirect("/globalconfig/productcategory/index");
	}
}

